package com.genesis.yunnanji.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.TabAdapter;
import com.genesis.yunnanji.fragment.Search_House;
import com.genesis.yunnanji.fragment.Search_Information;
import com.genesis.yunnanji.fragment.Search_Special;
import com.genesis.yunnanji.fragment.Search_Strategy;
import com.genesis.yunnanji.fragment.Search_Sup;
import com.jaeger.library.StatusBarUtil;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;



public class Search extends BaseActivity {
    @ViewInject(R.id.tb_search_toolbar)
    private Toolbar mToolbar;
    @ViewInject(R.id.tl_search_tab)
    private TabLayout mTablayout;
    @ViewInject(R.id.vp_search_viewpager)
    private ViewPager mViewPager;
    @ViewInject(R.id.et_search_search)
    private EditText editText;
    private TabAdapter adapter;
    public String[] tabTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_search);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.main_color), 2);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        editText.requestFocus();

    }


    @Override
    protected void setUpView() {
        tabTitle = new String[]{"资讯", "体验", "特产", "地产", "体验店"};
        mTablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTablayout.setTabGravity(TabLayout.GRAVITY_FILL);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(Search_Information.newInstance("资讯"));
        fragments.add(Search_Strategy.newInstance("体验"));
        fragments.add(Search_Special.newInstance("特产"));
        fragments.add(Search_House.newInstance("地产"));
        fragments.add(Search_Sup.newInstance("体验店"));
        adapter = new TabAdapter(getSupportFragmentManager(), fragments, tabTitle);

        mViewPager.setAdapter(adapter);
        mTablayout.setupWithViewPager(mViewPager);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    Search_Information search_information = (Search_Information) adapter.getItem(0);
                    search_information.setWd(editText.getText().toString());
                    Search_Strategy search_strategy = (Search_Strategy) adapter.getItem(1);
                    search_strategy.setWd(editText.getText().toString());
                    Search_Special search_special = (Search_Special) adapter.getItem(2);
                    search_special.setWd(editText.getText().toString());
                    Search_House search_house = (Search_House) adapter.getItem(3);
                    search_house.setWd(editText.getText().toString());
                    Search_Sup search_sup = (Search_Sup) adapter.getItem(4);
                    search_sup.setWd(editText.getText().toString());
                    return true;
                }
                return false;
            }
        });
    }
}
